﻿// Deep Earth is a community project available under the Microsoft Public License (Ms-PL)
// Code is provided as is and with no warranty - Use at your own risk
// View the project and the latest code at http://DeepEarth.codeplex.com/

using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;
using DeepEarth.Client.MapControl;
using DeepEarth.Client.MapControl.Geometries;
using DeepEarth.Client.MapControl.Layers;
using DeepEarth.Client.Services.NearMap;
using DeepEarth.Client.Services.Yahoo;
using DeepEarth.Client.Services.MapBox;
using DeepEarth.Client.Services.ArcGIS;
using DeepEarth.Client.Services.EarthAtlas;
using DeepEarth.Client.Services.MapsForFree;

namespace ExampleControl
{
    public partial class MainPage
    {
        public MainPage()
        {
            InitializeComponent();

            // MyMap.BaseLayer.Source = new ArcGisTileSource(ArcGISMapModes.DeLorme_World_Base_Map);
           // MyMap.BaseLayer.Source = new EarthAtlasTileSource(EarthAtlasModes.EarthLights);
            MyMap.BaseLayer.Source = new MapForFreeTileSource(MapForFreeMapModes.Relief);

            var geometryLayer = new GeometryLayer(MyMap);
            MyMap.Layers.Add(geometryLayer);

            var line1 = new LineStringControl { Locations = new ObservableCollection<Location> { new Location { Latitude = 15, Longitude = 15 }, new Location { Latitude = 30, Longitude = -15 }, new Location { Latitude = 45, Longitude = -15 } } };
            geometryLayer.Add(line1);

            var poly1 = new PolygonControl { Locations = new ObservableCollection<Location> { new Location { Latitude = 15, Longitude = 15 }, new Location { Latitude = 15, Longitude = -15 }, new Location { Latitude = -15, Longitude = -15 }, new Location { Latitude = -15, Longitude = 15 }, new Location { Latitude = 15, Longitude = 15 } } };
            geometryLayer.Add(poly1);

            var point1 = new PointControl { Location = new Location { Latitude = 15, Longitude = 15 } };
            geometryLayer.Add(point1);

            var transLayer = new GeometryLayer(MyMap) { UpdateMode = UpdateModes.TransformUpdate };
            MyMap.Layers.Add(transLayer);

            var point2 = new PointControl { Location = new Location { Latitude = 80, Longitude = 80 } };
            transLayer.Add(point2);

            var poly2 = new PolygonControl { Locations = new ObservableCollection<Location> { new Location { Latitude = 80, Longitude = 80 }, new Location { Latitude = 80, Longitude = 60 }, new Location { Latitude = 60, Longitude = 60 }, new Location { Latitude = 60, Longitude = 80 }, new Location { Latitude = 80, Longitude = 80 } } };
            transLayer.Add(poly2);

            var line2 = new LineStringControl { Locations = new ObservableCollection<Location> { new Location { Latitude = 60, Longitude = 60 }, new Location { Latitude = 50, Longitude = 40 }, new Location { Latitude = 60, Longitude = 20 } } };
            transLayer.Add(line2);
        }

        private void AddOpaicTileLayerButton_Click(object sender, RoutedEventArgs e)
        {
            var tl = new TileLayer(MyMap)
                     {
                         Source = new YhooTileSource(YhooMapModes.YahooStreet),
                         Opacity = 0.3D
                     };
            MyMap.Layers.Add(tl);
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var ops = new ChildWindow { MinHeight = 150, MinWidth = 250 };
            ops.Show();
        }
    }
}


// Set Map Tile Provider



//MyMap.BaseLayer.MSI.UseSprings = true;
//MyMap.BaseLayer.MSI.IsHitTestVisible = true;
//MyMap.BaseLayer.MSI.BlurFactor = 0.5D;

//MyMap.BaseLayer.Source = new GoogleTileSource(GoogleMapModes.Physical);

//var tl = new TileLayer(MyMap);
//tl.Source = new OsmTileSource(OsmMapModes.Osmarend);
//tl.MSI.UseSprings = false;
//tl.MSI.IsHitTestVisible = false;
//tl.MSI.BlurFactor = 0.5D;
//tl.Opacity = 0.4D;

//MyMap.Layers.Add(tl);


// MyMap.BaseLayer.Source = new GoogleTileSource(GoogleMapModes.Satellite);
// MyMap.BaseLayer.MSI.UseSprings = true;
// MyMap.BaseLayer.MSI.IsHitTestVisible = true;
// MyMap.BaseLayer.MSI.BlurFactor = 0.4D;

// var tl = new TileLayer(MyMap) { Source = new OsmTileSource(OsmMapModes.Osmarend), Opacity = 0.4D };
// tl.MSI.UseSprings = false;
// tl.MSI.IsHitTestVisible = false;

// MyMap.Layers.Add(tl);


////  TODO: implement into 
// MyMap.BaseLayer.MSI.GetViewportChanged().Subscribe(@event =>
// {
//     tl.MSI.ViewportOrigin = MyMap.BaseLayer.MSI.ViewportOrigin;
//     tl.MSI.ViewportWidth = MyMap.BaseLayer.MSI.ViewportWidth;
// });

//var geometryLayer = new GeometryLayer(MyMap);
//MyMap.Layers.Add(geometryLayer);

//var noobiePoint = new PointControl { X = 15, Y = 15 };
//geometryLayer.Add(noobiePoint);


//var panLayer = new GeometryLayer(MyMap) { UpdateMode = UpdateModes.PanOnlyUpdate };
//MyMap.Layers.Add(panLayer);

//var noobiePoint2 = new PointControl { X = 45, Y = 45 };
//geometryLayer.Add(noobiePoint2);


//var transLayer = new GeometryLayer(MyMap) { UpdateMode = UpdateModes.TransformUpdate };
//MyMap.Layers.Add(transLayer);

//var noobiePoint3 = new PointControl { X = 80, Y = 80 };
//geometryLayer.Add(noobiePoint3);


//private GeometryLayer _anchorTestLayer;
//private Style _styleArrow;

//private void GeometryLayerTest()
//{
//    //Bermuda Triangle is drawn on a GeometryLayer with TransformUpdate
//    var transformLayer = new GeometryLayer(MyMap)
//                         {
//                             UpdateMode = GeometryLayer.UpdateModes.TransformUpdate,
//                             Opacity = 0.5
//                         };

//    MyMap.Layers.Add(transformLayer);

//    var fillBrush = new RadialGradientBrush();
//    fillBrush.GradientStops.Add(new GradientStop { Color = Colors.Red, Offset = 0 });
//    fillBrush.GradientStops.Add(new GradientStop { Color = Colors.Purple, Offset = 1 });

//    var polygon = new Polygon
//                  {
//                      Fill = fillBrush,
//                      Points = new ObservableCollection<Point>
//                               {
//                                   new Point(-80.195, 25.775),
//                                   new Point(-64.75, 32.303),
//                                   new Point(-66.073, 18.44)
//                               }
//                  };

//    // Color.FromArgb(0x7F, 0xFF, 0x00, 0x00);
//    ToolTipService.SetToolTip(polygon, "Bermuda Triangle");

//    transformLayer.Add(polygon);



//    //Equator is drawn on a PanOnly GeometryLayer
//    var panOnlyLayer = new GeometryLayer(MyMap)
//                       {
//                           UpdateMode = GeometryLayer.UpdateModes.PanOnlyUpdate,
//                           Opacity = 0.5
//                       };

//    MyMap.Layers.Add(panOnlyLayer);

//    var line = new LineString
//               {
//                   LineThickness = 4,
//                   LineColor = Colors.White
//               };

//    line.Points.Add(new Point(-180, 0));
//    line.Points.Add(new Point(180, 0));
//    ToolTipService.SetToolTip(line, "Equator");

//    panOnlyLayer.Add(line);


//    //Equator endpoints are drawn on an ElementUpdate Geometry Layer to test synching of layers
//    var elementUpdateLayer = new GeometryLayer(MyMap)
//                             {
//                                 UpdateMode = GeometryLayer.UpdateModes.ElementUpdate,
//                                 Opacity = 0.5
//                             };

//    MyMap.Layers.Add(elementUpdateLayer);
//    elementUpdateLayer.Add(new PointBase { Point = new Point(-180, 0) });
//    elementUpdateLayer.Add(new PointBase { X = 180, Y = 0 }); // Alternative syntax for setting a PointBase

//    // New Location Base - like PointBase but uses Latitude & Longitude, not X,Y
//    elementUpdateLayer.Add(new LocationBase { Latitude = -45, Longitude = 45 });
//}

//private void GeometryAnchorTest()
//{
//    var point = new Point(0, 0);

//    if (_anchorTestLayer == null)
//    {
//        _anchorTestLayer = new GeometryLayer(MyMap)
//                           {
//                               ID = "RESULTSLAYER",
//                               UpdateMode = GeometryLayer.UpdateModes.ElementUpdate
//                           };

//        _styleArrow = (Application.Current.Resources["PushPinArrowStyle"]) as Style;
//        MyMap.Layers.Add(_anchorTestLayer);
//    }

//    //Add an arrow pin whose another point is an anchor of 0.5, 1.0;
//    var arrowPin = new PointBase { Point = point, Style = _styleArrow };
//    _anchorTestLayer.Add(arrowPin);

//    //Add a another point with the default style which has an anchor of 0.5, 0.5;
//    //PointBase defaultPin = new PointBase { Point = point };
//    //anchorTestLayer.Add(defaultPin);
//}